<?php
class ModelCommunityCommunity extends Model {
public function getTotalCommunitys() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_community`");
		return $query->row['total'];
	}
	
	private function filterSql($data){
		$sql ="";
		
		if(isset($data['filter_title'])&&!empty($data['filter_title'])){
			$sql .= " and title like '%".$data['filter_title']."%'";
		}
		
		if(isset($data['filter_news_id'])&&!empty($data['filter_news_id'])){
			$sql .= " and news_id='".(int)$data['filter_news_id']."'";
		}
		
		return $sql;
		
	}
	
	
	public function getRelatedArticles($data=array()){
		$sql ="select * from ".DB_PREFIX."message_article_to_news matn left join ".DB_PREFIX."message_news_article mna on(matn.news_article_id=mna.news_article_id) where 1=1 ";
		
		if(isset($data['filter_news_id'])&&!empty($data['filter_news_id'])){
			$sql .=" and matn.news_id='".(int)$data['filter_news_id']."'";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getAllCommunitys(){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_community`  where 1=1";
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getCommunitys($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_community`  where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getCommunity($community_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_community`  where community_id='".(int)$community_id."'";
		return $this->db->query($sql)->row;
	}
	
	public function addCommunity($data= array()){
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl =  'image/'.$data['pic_url'];
		} 
		$this->db->query("insert into `".DB_PREFIX."config_community` set title='".$data['title']."',description='".$data['description']."',pic_url='".$data['pic_url']."',ab_pic_url='".$abpicUrl."',url='".$data['url']."',url_type='".(int)$data['url_type']."',summary='".$data['summary']."',lat='".doubleval($data['lat'])."',lng='".doubleval($data['lng'])."'");
		$lastArticleId = (int)$this->db->getLastId();
		if(!$data['url_type']){
			$url = "index.php?route=".$data['url'].'&community_id='.(int)$lastArticleId;
			$this->db->query("update `".DB_PREFIX."config_community` set url_type='".(int)$data['url_type']."',url='".$url."' where community_id='".(int)$lastArticleId."'");
		}
	}
	
	
	public function editPolice($community_id,$data=array()){
		$sql ="update ".DB_PREFIX."config_police set community_id=0 where community_id='".(int)$community_id."'";
		$this->db->query($sql);
		if(isset($data['police_route'])){
			foreach ($data['police_route'] as $police_id) {
				$this->db->query('update  '.DB_PREFIX."config_police set community_id='".(int)$community_id."'  where police_id='".(int)$police_id."'");
			}
		}
	}
	
	public function editCommunity($community_id,$data=array()){
		if(strpos($data['url'], "index.php")===0){
			$url = $data['url'];
		}else{
			$url = "index.php?route=".$data['url'].'&community_id='.(int)$community_id;
		}
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl =  'image/'.$data['pic_url'];
		} 
		$sql = "update ".DB_PREFIX."config_community set title='".$data['title']."',description='".$data['description']."',pic_url='".$data['pic_url']."',ab_pic_url='".$abpicUrl."',url='".$url."',url_type='".(int)$data['url_type']."',summary='".$data['summary']."',lat='".doubleval($data['lat'])."',lng='".doubleval($data['lng'])."' where community_id='".(int)$community_id."'";
		$this->db->query($sql);
	}
	
	public function deleteCommunity($community_id){
		$sql ="delete  from ".DB_PREFIX."config_community where community_id='".(int)$community_id."'";
		$this->db->query($sql);
	}
}
?>